<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_m
 *
 * @author QUYET
 */
class admin_m extends CI_Model{
    var $language = 1;
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
        if($this->session->userdata('lang')=='en') $this->language = 2;
    }
    function get_all_categories($per_page=10,$off_set=0){
        $this->db->select('*');
        $this->db->from('ots_ticket_cat');
        $this->db->join('ots_ticket_cat_lang', 'ots_ticket_cat.id=ots_ticket_cat_lang.id_category', 'inner');
        $this->db->where('ots_ticket_cat_lang.id_lang',$this->language);
        $this->db->limit($per_page,$off_set);
        return $this->db->get()->result_array();
    }
    function add_category($data,$lang='fr'){
        
        $record_update_main = array();
        if(isset($data['parent_id'])) {
            $record_update_main['parent_id'] = $data['parent_id'];
            unset($data['parent_id']);
        }
        if(isset($data['depth'])) {
            $record_update_main['depth'] = $data['depth'];
            unset($data['depth']);
        }
        if(empty($record_update_main))  return false;
        $this->db->insert('ticket_cat',$record_update_main);        
        $id_cat = $this->db->insert_id();
        $data['id_category'] = $id_cat;
        $data['id_lang'] = $this->language;
        return $this->db->insert('ticket_cat_lang',$data);
    }
    function get_cat_details($id_cat, $uselang = false){
        $this->db->select('*');
        $this->db->from('ots_ticket_cat');
        $this->db->join('ots_ticket_cat_lang', 'ots_ticket_cat.id=ots_ticket_cat_lang.id_category', 'inner');
        $this->db->where('ots_ticket_cat.id',$id_cat);
        if($uselang) {
            $this->db->where('ots_ticket_cat_lang.id_lang',$this->language);
            return $this->db->get()->row();
        }
        else{
            $one_cate_alldetail = $this->db->get()->result();
            if(count($one_cate_alldetail)==2){
                foreach($one_cate_alldetail as $catlg){
                    if($catlg->id_lang==$this->language) return $catlg;
                }
            }
            elseif(!empty($one_cate_alldetail[0])) return $one_cate_alldetail[0];
            else return false;
        }
        //return $this->db->get()->row();
    }
    
    function update_category($data,$id_cat){
        $record_update_main = array();
        if(isset($data['parent_id'])) {
            $record_update_main['parent_id'] = $data['parent_id'];
            unset($data['parent_id']);
        }
        if(isset($data['depth'])) {
            $record_update_main['depth'] = $data['depth'];
            unset($data['depth']);
        }
        if(!empty($record_update_main)) $this->db->update('ots_ticket_cat',$record_update_main,array('id'=>$id_cat));        
        $get_one_lange = $this->get_cat_details($id_cat,true);
        if(!empty($get_one_lange)) return $this->db->update('ticket_cat_lang',$data,array('id_category'=>$id_cat,'id_lang'=>$this->language));
        else{
            $data['id_category'] = $id_cat;
            $data['id_lang'] = $this->language;
            return $this->db->insert('ticket_cat_lang',$data);
        }
        
    }
    
    function get_all_tickets($id_category=1,$per_page=10,$off_set=0){
        $this->db->select('ots_ticket.id,ticket_lang.ticket_name');
        $this->db->from('ots_ticket');
        $this->db->join('ticket_lang', 'ticket.id=ticket_lang.id_ticket', 'inner');
        $this->db->where('ots_ticket.cat_id',$id_category);
        return $this->db->get()->result_array();
    }
}

?>
